#include "../common.h"

/*
折半查找的递归算法
compare:返回0表示相等，返回>0的数表示c1>c2，返回<0的数则反之亦然。算法默认数组为升序，可通过compare函数自行调节。
返回值:-1表示查找失败
*/
int BinarySearchRecursively(void** array,int low,int high,void* key,int (*compare)(void* c1,void* c2)){
    if(low > high)
        return -1;
    int mid = (low + high) / 2;
    int result = compare(key,array[mid]);
    if(result == 0){//mid == key
        return mid;
    }else if(result > 0){//mid > key
        return BinarySearchRecursively(array,mid + 1,high,key,compare);
    }else{// mid < key
        return BinarySearchRecursively(array,low,high - 1,key,compare);
    }
}